package com.imay.live.view.dialog;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.widget.Button;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.imay.base.fragment.BaseDialogFragment;
import com.imay.live.adapter.ChoiseDialogAdapter;
import com.imay.live.helper.DialogFragmentHelper;
import com.imay.live.model.MenuItemModel;
import com.imay.live.R;
import com.imay.live.utils.RecyclerViewUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 选择框
 * Created by chan on 2017/4/11.
 */
public class ChoiseDialogFragment extends BaseDialogFragment {
    private static ChoiseDialogFragment dialog;

    private RecyclerView mRecyclerView;
    private ChoiseDialogAdapter mAdapter;
    private Button mTvChoise;
    private List<MenuItemModel> datas = new ArrayList<>();

    protected Animation innerShowAnim;
    protected Animation innerDismissAnim;

    // 区分标志
    private int tagCallBack = -1;

    private OnItemClickListener listener;


    public void setTagCallBack(int tagCallBack) {
        this.tagCallBack = tagCallBack;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.listener = onItemClickListener;
    }

    public ChoiseDialogFragment() {
    }

    public interface OnItemClickListener {
        /**
         * 回调
         *
         * @param tagCallBack 区分不同dialog共同回调的标志
         * @param position    对应的位置
         * @param str         对应的字符串
         */
        void itemClick(int tagCallBack, int position, MenuItemModel str);
    }


    public static ChoiseDialogFragment newInstance() {
        ChoiseDialogFragment instance = new ChoiseDialogFragment();
        return instance;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_choise;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        Window window = getDialog().getWindow();
        window.setGravity(Gravity.BOTTOM); //设置dialog的位置
        window.getAttributes().windowAnimations = R.style.bottomtotopanim;

        // 加载数据
        mRecyclerView = $(R.id.rcv_dialog_choise);
        RecyclerViewUtil.setLinearLayoutManager(mRecyclerView.getContext(),mRecyclerView);
        mTvChoise = $(R.id.tv_dialog_choise);
        mAdapter = new ChoiseDialogAdapter(datas);
        mTvChoise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        mRecyclerView.setAdapter(mAdapter);

        // 监听单击回调
        mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                dialog.dismiss();
                if (listener != null) {
                    listener.itemClick(tagCallBack, position, datas.get(position));
                }
            }
        });
    }

    /**
     * 弹出选择框
     *
     * @param fragmentManager
     * @param strings         字符串list
     * @param tagCallBack     标志
     * @param listener        回调
     */
    public static void show(FragmentManager fragmentManager, List<MenuItemModel> strings, int tagCallBack, OnItemClickListener listener) {
        show(fragmentManager, strings, tagCallBack, listener, null);
    }

    /**
     * 弹出选择框
     *
     * @param fragmentManager
     * @param strings         字符串list
     * @param tagCallBack     标志
     * @param listener        回调
     * @param buttomText      底部按钮的内容
     */
    public static void show(FragmentManager fragmentManager, List<MenuItemModel> strings, int tagCallBack, OnItemClickListener listener, String buttomText) {
        if (dialog == null) {
            dialog = ChoiseDialogFragment.newInstance();
        }
        dialog.setNewDatas(strings);
        dialog.setTagCallBack(tagCallBack);
        dialog.setOnItemClickListener(listener);
        if (!TextUtils.isEmpty(buttomText)) {
            dialog.setButton(buttomText);
        }
        DialogFragmentHelper.showDialogFragment(fragmentManager, dialog, "choise");
    }

    @Override
    public void onStart() {
        super.onStart();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
    }

    public void setNewDatas(List<MenuItemModel> newDatas) {
        this.datas = newDatas;
    }

    public void setButton(String button) {
        mTvChoise.setText(button);
    }

    protected boolean isOutsideTouchable() {
        return true;
    }
}
